package com.medicine.hbp.ui;

import org.kymjs.kjframe.http.HttpParams;
import org.kymjs.kjframe.ui.BindView;

import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;

import com.medicine.hbp.adapter.FragmentAdapter01;
import com.medicine.hbp.base.BaseActivity;
import com.medicine.hbp.http.AppHttpCallBack;
import com.medicine.hbp.http.AppHttps;
import com.medicine.hbp.http.HttpInterface;
import com.medicine.hbp.mob.artGroupListVo;
import com.medicine.hbp.uitl.CommUtil;
import com.medicine.hbp.uitl.ToastUtil;
import com.medicine.hbp.xListView.XListView;
import com.medicine.hbp.R;
/**
 * 搜索界面
 * @author Administrator
 */
public class SearchActivity extends BaseActivity{
	private artGroupListVo result;
	@BindView(id = R.id.edit_search, click = false)
	public EditText edit_search;
	@BindView(id = R.id.xListView, click = false)
	public XListView xlistview;
	@BindView(id = R.id.btn_search, click = true)
	public ImageView btn_search;

	private FragmentAdapter01 adapter;
	@Override
	public void setRootView() {
		super.setRootView();
		setContentView(R.layout.activity_search);
	}
	@Override
	public void initWidget() {
		super.initWidget();
		xlistview.setPullLoadEnable(false);
		xlistview.setPullRefreshEnable(false);
		result=(artGroupListVo) getIntent().getSerializableExtra("result");
		adapter = new FragmentAdapter01(aty);
		xlistview.setAdapter(adapter);
		adapter.addItems(result.artInfo);
		adapter.notifyDataSetChanged();
		initTop();
	}
	@Override
	public void widgetClick(View v) {
		super.widgetClick(v);
		switch (v.getId()) {
			case R.id.btn_search:
				String context=edit_search.getText().toString();
				if(TextUtils.isEmpty(context)){
					ToastUtil.showShort(aty, "请输入搜索内容");
					return;
				}
				httpSearchLaber(context);
				break;
		}
	}
	private void initTop() {
		TopView topview = (TopView) findViewById(R.id.topview);
		topview.setTitle("搜索结果");
		topview.setLeftVisibility(View.GONE);
		topview.setLeftVisibilityExit(View.VISIBLE);
		topview.setRightVisibility(View.INVISIBLE);
		topview.setLeftOnClickExit(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
	}
	/**
	 * 搜索
	 */
	private void httpSearchLaber(String context) {
		HttpParams params = new HttpParams();
		params.put("keyword", context);//用户输入需要搜索的文字
		AppHttps.HttpPost(this,HttpInterface.SEARCH_LABER, params,
				new AppHttpCallBack<artGroupListVo>(artGroupListVo.class,
						aty) {
					@Override
					public void onSuccess(artGroupListVo result) {
						adapter.addItems(result.artInfo);
						adapter.notifyDataSetChanged();

					}

					@Override
					public void onFailures(int errorCode, String errorMsg) {
						ToastUtil.showShort(aty, errorMsg);
					}

					@Override
					public void onFinish() {
						super.onFinish();
						xlistview.setRefreshTime(CommUtil.formatDate(System
								.currentTimeMillis()));
					}
				});

	}
}
